import random

from appium import webdriver
import time
from appium.options.android import UiAutomator2Options

def get_charlist(amount):

    clist = []
    for c in amount:
        clist.append(c)
    return clist

if __name__ == '__main__':
    options = UiAutomator2Options()
    options.platform_name = 'Android'
    options.platform_version = '7.1.2'
    options.device_name = 't1'
    options.udid = '127.0.0.1:62001'
    options.automation_name = 'UiAutomator2'
    options.app_package = 'com.mobivans.onestrokecharge'
    options.app_activity = 'com.stub.stub01.Stub01'

    # 生成appium驱动对象
    wd = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=options)
    # wd.maximize_window()  # 没有用
    wd.implicitly_wait(5)  # 有用
    time.sleep(8)
    wd.find_element('xpath', "//android.widget.TextView[@text='记一笔']").click()
    # //android.widget.LinearLayout[@index='22']
    random_index = random.randint(0,26)
    wd.find_element('xpath', f"//android.widget.LinearLayout[@index='{random_index}']").click()
    time.sleep(2)
    amount = input('请输入支出金额')
    clist = get_charlist(amount)
    for c in clist:
        if c == '.':
            wd.find_element('xpath', "//android.widget.Button[@text='.']").click()
        else:
            wd.find_element('xpath', f"//android.widget.Button[@text={c}]").click()
        time.sleep(1)
    wd.find_element('xpath', "//android.widget.Button[@text='完成']").click()
    time.sleep(1)
    wd.find_element('xpath', "//android.widget.TextView[@text='记一笔']").click()
